 
var express = require('express');
var app = express();
var http = require('http').Server(app); //将express注册到http中
var io = require('socket.io')(http);
//当访问根目录时，返回Hello World
app.get('/', function (req, res) {
    
    res.sendFile(__dirname+"/index.html");
});

var usocket = [];
io.on('connection',function(socket){
    console.log('a user connect');

    socket.on("join", function (name) {
        usocket[name] = socket;
        console.log(usocket);
        io.emit("join", name) //服务器通过广播将新用户发送给全体群聊成员
    })

    //new addition
    socket.on("message", function (msg) {
        io.emit("message", msg) //将新消息广播出去
    })

});

//启动监听，监听3000端口
http.listen(8080, function () {
    console.log('listening on *:8080');
});

